﻿/*
Copyright (c) 2009 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System;
using TeachMe.Application.Properties;

namespace TeachMe.Application.Forms
{
    public partial class AboutForm : PseudoModalForm, OnlineVersionChecker.IVersionDataReceiver
    {
        private delegate void UpdateNewVersionStatusDelegate();

        private enum NewVersionCheckState
        {
            Checking,
            NewVersionAvailable,
            NoNewVersion,
            Failed
        }

        private readonly Root _root;
        private NewVersionCheckState _newVersionCheckState;
        private OnlineVersion _onlineVersion;

        private object _newVersionCheckStateLock = new object();
        private object _onlineVersionLock = new object();

        public AboutForm(Root root)
        {
            _root = root;
            _newVersionCheckState = NewVersionCheckState.Checking;

            InitializeComponent();
            UpdateNewVersionStatus();
            OnlineVersionChecker.Check(this, _root.CurrentVersion, _root.UserSettings.Id.Value, _root.UserSettings.Language.Value);
        }

        private void UpdateNewVersionStatus()
        {
            NewVersionCheckState localNewVersionCheckState;

            lock (_newVersionCheckStateLock)
            {
                localNewVersionCheckState = _newVersionCheckState;
            }

            switch (localNewVersionCheckState)
            {
                case NewVersionCheckState.Checking:
                    txVersion.Text = Resources.CheckingNewVersion;
                    pbVersionCheckStatus.Image = Resources.loading;
                    break;
                case NewVersionCheckState.Failed:
                    txVersion.Text = Resources.ErrorGettingNewVersion;
                    pbVersionCheckStatus.Image = Resources.warning;
                    break;
                case NewVersionCheckState.NewVersionAvailable:
                    OnlineVersion onlineVersionCopy;
                    lock (_onlineVersionLock)
                    {
                        onlineVersionCopy = _onlineVersion.Copy();
                    }
                    // no need to revert values
                    txVersion.Text = String.Format(Resources.NewVersionIsAvailable, onlineVersionCopy.Value);
                    txVersion.Cursor = System.Windows.Forms.Cursors.Hand;
                    pbVersionCheckStatus.Image = Resources.present;
                    txVersion.Click += delegate { _root.DisplayOnlineVersionInfo(onlineVersionCopy, this); };
                    break;
                case NewVersionCheckState.NoNewVersion:
                    txVersion.Text = String.Format(Resources.VersionIsUpToDate, _root.CurrentVersion);
                    pbVersionCheckStatus.Image = Resources.ok;
                    break;
            }
        }

        private void OnPictureClick(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.teachme.cz");
        }

        public void Process(OnlineVersionChecker.Result result, OnlineVersion onlineVersion = null)
        {
            lock (_newVersionCheckStateLock)
            {
                switch (result)
                {
                    case OnlineVersionChecker.Result.Failed:
                        _newVersionCheckState = NewVersionCheckState.Failed;
                        break;
                    case OnlineVersionChecker.Result.UpToDate:
                        _newVersionCheckState = NewVersionCheckState.NoNewVersion;
                        break;
                    case OnlineVersionChecker.Result.New:
                        _newVersionCheckState = NewVersionCheckState.NewVersionAvailable;
                        break;
                }
            }

            lock (_onlineVersionLock)
            {
                _onlineVersion = onlineVersion;
            }

            if (InvokeRequired)
            {
                BeginInvoke(new UpdateNewVersionStatusDelegate(UpdateNewVersionStatus));
            }
            else
            {
                UpdateNewVersionStatus();
            }
        }

        private void OnLicenceClick(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
        {
            _root.FormsManager.ShowMemoForm(this, Resources.License, Utilities.ReadTextResource("TeachMe.Application.Embedded.License.txt"));
        }

        private void OnCreditsClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
        {
            _root.FormsManager.ShowMemoForm(this, Resources.Credits, Utilities.ReadTextResource("TeachMe.Application.Embedded.Credits.txt"));
        }
    }
}
